<template>
  <div>
    <div class="title">
      公司资料
    </div>
    <div class="fromData">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <div style="text-align: center;margin-bottom: 15px;">
          <el-upload action="/api/upload" :show-file-list="false" :limit="1" :on-success="handleSuccessotherimg"
            :before-upload="beforeUpload">
            <img class="upImg"  :src="avatarUrl"  style="width: 80px; height: 80px; border-radius: 50%;"/>
          </el-upload>
          <div>
            提示：上传本人近期照片,
          </div>
          <div>提高你在行业中的诚信形象和亲和力,有助于提升你本人和公司的诚信分值</div>
        </div>
        <el-form-item label="公司名称">
          <el-input v-model="form.company_title"></el-input>
        </el-form-item>
        <el-form-item label="联系人姓名：" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码：" prop="contact_phone">
          <el-input v-model="form.contact_phone"></el-input>
        </el-form-item>
        <el-form-item label="联系电话：">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="微信号：">
          <el-input v-model="form.weixin"></el-input>
        </el-form-item>
        <el-form-item label="邀请码：">
          <span>{{ invitationCode }}</span>
          <el-button class="yqButton" type="primary" @click="newShareOn">我要邀请</el-button>
          
        </el-form-item>
        <div style="margin-left: 100px;">
            将“分享码”分享给好友同事
          </div>
          <div style="margin-left: 100px;">惊喜的分润——让工作与快乐同行；</div>
          <div style="margin-left: 100px;">彰显你在船供行业内的人脉和威望；</div>
        <el-form-item>
          <el-button class="buttonClose" @click="cancel('form')">取消</el-button>
          <el-button class="buttonPay" type="primary" @click="save('form')">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 分享 -->
    <newShare ref="newShare"></newShare>
  </div>
</template>

<script>
import newShare from "../newShare.vue";

import { mapState } from 'vuex';

export default {
  name: "company",
  components: {
    newShare
  },
  data() {
    return {
      invitationCode: '',
      avatarUrl: 'https://www.keaitupian.cn/cjpic/frombd/0/253/3680147856/2045951790.jpg', // 请替换为实际头像地址
      form: {
        company_title: "",
        image: "",
        name: "",
        contact_phone: "",
        mobile: "",
        weixin: "",
        email: ""
      },

      rules: {
        name: [
          { required: true, message: '请输入联系姓名', trigger: 'blur' },
        ],
        contact_phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
        ],
      }

    }
  },
  computed: {
    ...mapState([
      "userInfo",
    ]),
  },

  created() {
    console.log(this.userInfo)

    this.form = this.userInfo;
    this.avatarUrl = this.userInfo.image || 'https://www.keaitupian.cn/cjpic/frombd/0/253/3680147856/2045951790.jpg'; // 默认头像地址
    this.getShareData();

  },
  methods: {
    beforeUpload(file) {
      // 上传前的处理逻辑，比如文件大小限制等
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error('文件大小不能超过10MB!');
      }
      return isLt2M;
    },

    handleSuccessotherimg(response, file, fileList) {
      this.avatarUrl = response.data.path
    },
    getShareData() {
      this.$httpApi("/api/share", {
        token: this.userInfo.token,
      }, 'get').then(res => {
        this.invitationCode = res.data.code;
      })
    },
    newShareOn() {
      if (this.userInfo.beFx == 1) {
        this.$refs.newShare.isVisible = true;
      } else {
        this.$httpApi("/api/toBeAgent", {
          token: this.userInfo.token,
        }, 'get').then(res => {          
          if (res.code == 200) {
            alertSucc('申请成功，请等待审核');
          } else {
            alertErr(res.msg);
          }
        })
      }
    },
    invite() {
      console.log('触发我要邀请操作');
      // 这里添加具体的邀请逻辑，比如发送请求等
    },
    cancel(formName) {
      this.form = this.userInfo
      this.$refs[formName].resetFields();
      // 可以添加重置表单等逻辑
      console.log('触发取消操作');
      // this.form = {
      //   company_title:"",
      //   image:"",
      //   name:"",
      //   contact_phone:"",
      //   mobile:"",
      //   weixin:"",
      //   email:""
      // }
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$httpApi("/api/infoSave", {
            ...this.form,
            image: this.avatarUrl,
          }, "post").then(res => {
            if (res.code == 200) {
              alertSucc(res.msg);
              console.log(res)
              this.$store.dispatch("getUserloginedInfo");
            } else {
              alertErr(res.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      console.log('触发保存操作');
      // 这里添加保存表单数据的逻辑，比如发送请求到后端
    }
  }

}
</script>

<style lang="less" scoped>
.buttonPay {
  width: 133px;
  height: 40px;
  background: #0052D9;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid rgba(0, 0, 0, 0);

  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;

  font-style: normal;
  text-transform: none;
}

.buttonClose {
  margin-top: 50px;
  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 400;
  font-size: 14px;
  color: #666666;
  margin-left: 120px;
  font-style: normal;
  text-transform: none;
  width: 133px;
  height: 40px;
  background: #F5F5F5;
  border-radius: 4px 4px 4px 4px;
  border: 1px solid #D7D7D7;
}

.yqButton {
  width: 133px;
  height: 40px;
  background: #FF6F00;
  border-radius: 20px 20px 20px 20px;
  border: 1px solid rgba(0, 0, 0, 0);
  margin-left: 21px;
}

.fromData {
  width: 732px;
  //   min-height: 715px;
  margin: auto;
  margin-top: 40px;
  padding-bottom: 20px;
}

.title {
  height: 56px;
  background: #FFFFFF;
  line-height: 56px;
  border-radius: 0px 0px 0px 0px;
  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: bold;
  font-size: 16px;
  color: #333333;
  font-style: normal;
  text-transform: none;
  border-bottom: 4px solid #F5F5F5;
  padding-left: 32px;
}
</style>